home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / libtiff / tif_next.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  140 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_next.c,v 1.18 92/10/21 16:36:23 sam Rel $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * TIFF Library.
  31.  *
  32.  * NeXT 2-bit Grey Scale Compression Algorithm Support
  33.  */
  34. #include "tiffiop.h"
  35.  
  36. #define SETPIXEL(op, v) {            \
  37.     switch (npixels++ & 3) {        \
  38.     case 0:    op[0]  = (v) << 6; break;    \
  39.     case 1:    op[0] |= (v) << 4; break;    \
  40.     case 2:    op[0] |= (v) << 2; break;    \
  41.     case 3:    *op++ |= (v);       break;    \
  42.     }                    \
  43. }
  44.  
  45. #define LITERALROW    0x00
  46. #define LITERALSPAN    0x40
  47. #define WHITE       ((1<<2)-1)
  48.  
  49. static int
  50. DECLARE4(NeXTDecode, TIFF*, tif, u_char*, buf, u_long, occ, u_int, s)
  51. {
  52.     register u_char *bp, *op;
  53.     register int cc, n;
  54.     u_char *row;
  55.     int scanline;
  56.  
  57.     /*
  58.      * Each scanline is assumed to start off as all
  59.      * white (we assume a PhotometricInterpretation
  60.      * of ``min-is-black'').
  61.      */
  62.     for (op = buf, cc = occ; cc-- > 0;)
  63.         *op++ = 0xff;
  64.  
  65.     bp = (u_char *)tif->tif_rawcp;
  66.     cc = tif->tif_rawcc;
  67.     scanline = tif->tif_scanlinesize;
  68.     for (row = buf; (long)occ > 0; occ -= scanline, row += scanline) {
  69.         n = *bp++, cc--;
  70.         switch (n) {
  71.         case LITERALROW:
  72.             /*
  73.              * The entire scanline is given as literal values.
  74.              */
  75.             if (cc < scanline)
  76.                 goto bad;
  77.             memcpy(row, bp, scanline);
  78.             bp += scanline;
  79.             cc -= scanline;
  80.             break;
  81.         case LITERALSPAN: {
  82.             int off;
  83.             /*
  84.              * The scanline has a literal span
  85.              * that begins at some offset.
  86.              */
  87.             off = (bp[0] * 256) + bp[1];
  88.             n = (bp[2] * 256) + bp[3];
  89.             if (cc < 4+n)
  90.                 goto bad;
  91.             memcpy(row+off, bp+4, n);
  92.             bp += 4+n;
  93.             cc -= 4+n;
  94.             break;
  95.         }
  96.         default: {
  97.             register int npixels = 0, grey;
  98.             int imagewidth = tif->tif_dir.td_imagewidth;
  99.  
  100.             /*
  101.              * The scanline is composed of a sequence
  102.              * of constant color ``runs''.  We shift
  103.              * into ``run mode'' and interpret bytes
  104.              * as codes of the form <color><npixels>
  105.              * until we've filled the scanline.
  106.              */
  107.             op = row;
  108.             for (;;) {
  109.                 grey = (n>>6) & 0x3;
  110.                 n &= 0x3f;
  111.                 while (n-- > 0)
  112.                     SETPIXEL(op, grey);
  113.                 if (npixels >= imagewidth)
  114.                     break;
  115.                 if (cc == 0)
  116.                     goto bad;
  117.                 n = *bp++, cc--;
  118.             }
  119.             break;
  120.         }
  121.         }
  122.     }
  123.     tif->tif_rawcp = (char *)bp;
  124.     tif->tif_rawcc = cc;
  125.     return (1);
  126. bad:
  127.     TIFFError(tif->tif_name, "NeXTDecode: Not enough data for scanline %d",
  128.         tif->tif_row);
  129.     return (0);
  130. }
  131.  
  132. int
  133. DECLARE1(TIFFInitNeXT, TIFF*, tif)
  134. {
  135.     tif->tif_decoderow = NeXTDecode;
  136.     tif->tif_decodestrip = NeXTDecode;
  137.     tif->tif_decodetile = NeXTDecode;
  138.     return (1);
  139. }
  140.